package com.atguigu.distributed.lock.leecode;

import java.util.Stack;

/**
 * 实现一个栈，入栈，出栈，取最小元素时间复杂度都是O(1)
 */
public class MyStack {
    private Stack<Integer> mainStack = new Stack<>();
    private Stack<Integer> minStack = new Stack<>();

    /**
     * 入栈操作
     * @param element
     */
    public void push(int element){
        mainStack.push(element);
        if(minStack.empty()||element<=minStack.peek()){
            minStack.push(element);
        }
    }

    /**
     * 出栈操作
     * @return
     */
    public Integer pop(){
        if(minStack.peek().equals(mainStack.peek())){
            minStack.pop();
        }
        return mainStack.pop();
    }
    public int getMin() throws Exception{
        if(minStack.empty()){
            throw new Exception("stack is empty");
        }
        return minStack.peek();
    }
}
